package com.blockmeta.bbs.businesslibrary.decoration;

import android.graphics.Rect;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;

import com.blockmeta.bbs.baselibrary.utils.DimentionsUtil;

/**
 * @author AirThor
 * @date 2018/10/23.
 */
public class GridSpaceDecoration extends RecyclerView.ItemDecoration {
    private int mPx = DimentionsUtil.dp2px(5);

    public void setPx(int px) {
        mPx = px;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        GridLayoutManager manager = (GridLayoutManager) parent.getLayoutManager();
        if (manager == null){
            return;
        }
        int span = manager.getSpanCount();
        int position = parent.getChildLayoutPosition(view);
        boolean isFirstRow = position < span;
        boolean isFirstColumn = position % span == 0;
//        if (!isFirstColumn) {
            outRect.left = mPx;
//        }
        if (isFirstRow) {
            outRect.top = 0;
        } else {
            outRect.top = mPx;
        }
    }
}
